#!/usr/bin/python
# -*- coding: utf-8 -*-
from flask import render_template, request, redirect, Blueprint
import equiputils
import json
from dbOperate import dbOperate

equip = Blueprint('equip', __name__)
equiputils.getequip()
db1 = dbOperate('10.37.133.21', 'spider', 'spider', 'spider')

@equip.route('/get_equip', methods=['GET'])
def get_equip():
    sql1 = 'select * from equip '
    see = db1.exe_query(sql1)
    hostname = []
    value = []
    jsonData = {}
    for data in see:
        hostname.append(data[0])
        value.append(data[1])

    jsonData['hostname'] = hostname
    jsonData['value'] = value
    j = json.dumps(jsonData, ensure_ascii=False)
    # print ('equip--------' + j)
    return (j)


@equip.route('/equiplist/')
def equiplist():
    userlist = equiputils.getequip().items()
    # print('userlist:%s' % userlist)
    return render_template('equiplist.html', userlist=userlist)


@equip.route('/equipupdate/')
def equipupdate():
    username = request.args.get('username')
    dit = equiputils.getequip()
    password = dit.get(username)
    user = [username, password]
    # print('update:%s' % user)
    return render_template('equipupdate.html', user=user)


@equip.route('/equipupdateaction/', methods=['POST'])
def equipupdateaction():
    params = request.args if request.method == 'GET' else request.form
    username = params.get('username')
    password = params.get('password')
    equiputils.file_dict[username] = password
    equiputils.equip_write()
    return redirect('/equiplist/')
